<?php

namespace backend\controllers;

/**
 * @author  shuang
 * @date    2017-1-4 23:39:32
 * @version V1.0
 * @desc    
 */
use common\components\BController;
use common\components\MTools;
use yii\helpers\Json;
use Yii;
use backend\models\MY_User;
use yii\helpers\ArrayHelper;

class BrushController extends BController {

    public function actions() {
        return [
            "list" => [
                "class" => "\common\actions\ListAction",
                "modelClass" => new MY_User()
            ],
            "detail" => [
                "class" => "\common\actions\ListAction",
                "getListFunc" => "getDetail",
                "iscallback" => true,
                "modelClass" => new MY_User(),
                "renderTo" => "detail"
            ],
            "merlist" => [
                "class" => "\common\actions\ListAction",
                "modelClass" => new MY_User(),
                "renderTo" => "merlist"
            ],
            "merdetail" => [
                "class" => "\common\actions\ListAction",
                "getListFunc" => "getDetail",
                "iscallback" => true,
                "modelClass" => new MY_User(),
                "renderTo" => "merdetail"
            ],
        ];
    }

    /*
     * ajax数据列表
     */

    public function actionAjaxlist() {
        $res = MY_User::getBrushList();
        $temp = ["total" => $res["total"], "rows" => []];
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $temp["rows"][] = [
                    "id" => $item["id"],
                    "username" => $item["username"],
                    "froststatusname" => MTools::setFontColor($item["froststatus"], MY_User::$froststatus[$item["froststatus"]]),
                    "froststatus" => $item["froststatus"],
                    "last_login_at" => Yii::$app->formatter->asDatetime($item["last_login_at"]),
                    "amount" => Yii::$app->formatter->asDecimal(ArrayHelper::getValue($item, "amount")),
                    "successcont" => Yii::$app->formatter->asInteger(MY_User::getCompleteTaskCount($item["id"])),
                    "tousucont" => Yii::$app->formatter->asInteger(MY_User::getcomplaintTaskCount($item["id"])),
                    "hiddencont" => Yii::$app->formatter->asInteger(MY_User::getHiddenTaskCount($item["id"])),
                    "refercont" => Yii::$app->formatter->asInteger(MY_User::getInviteTaskCount($item["my_code"])),
                    "action" => MTools::getStringActions([
                        'detail' => [
                            "params" => [ "id" => $item["id"]],
                            "title" => "查看详情"
                        ],
                        "shifttodrawal" => [
                            "options" => ["onclick" => "javascript:member.shifttodrawal(" . $item["id"] . ");"],
                            "title" => "余额转入提现"
                        ]
                            ], [ "shifttodrawal" => ["show" => true, "url" => "javascript:;"]])
                ];
            }
        }
        echo Json::encode($temp);
    }

    /*
     * ajax数据列表
     */

    public function actionAjaxmerlist() {
        $res = MY_User::getMerList();
        $temp = ["total" => $res["total"], "rows" => []];
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $action = MTools::getStringActions(
                                [
                            "merdetail" => [
                                "params" => [ "id" => $item["id"]],
                                "title" => "查看详情"
                            ],
                            "editgrade" => [
                                "options" => ["onclick" => "javascript:shopmember.editgrade(" . $item["id"] . ");"],
                                "title" => "更改级别"
                            ]
                                ], [ "editgrade" => ["show" => true, "url" => "javascript:;"]
                                ]
                );
                $temp["rows"][] = [
                    "id" => $item["id"],
                    "username" => $item["username"],
                    "froststatusname" => MTools::setFontColor($item["froststatus"], MY_User::$froststatus[$item["froststatus"]]),
                    "froststatus" => $item["froststatus"],
                    "grade" => $item["grade"],
                    "last_login_at" => Yii::$app->formatter->asDatetime($item["last_login_at"]),
                    "truename" => $item["truename"],
                    "amount" => Yii::$app->formatter->asDecimal(ArrayHelper::getValue($item, "amount")),
                    "rebate_amount" => Yii::$app->formatter->asDecimal(ArrayHelper::getValue($item, "rebate_amount")),
                    "created_at" => Yii::$app->formatter->asDatetime($item["regist_at"]),
                    "taskcont" => Yii::$app->formatter->asDecimal($item["taskcont"] ? $item["taskcont"] : 0, 0),
                    "action" => $action
                ];
            }
        }
        echo Json::encode($temp);
    }

    /*
     * 更改账户级别
     */

    public function actionEditgrade() {
        if (Yii::$app->request->isAjax) {
            $userid = Yii::$app->request->post("id");
            $gradeid = Yii::$app->request->post("gradeid");
            if ($gradeid > 0) {
                $userprofile = \backend\models\MY_UserProfile::find()->where("userid=:userid", [":userid" => $userid])->one();
                $userprofile->gradeid = $gradeid;
                if ($userprofile->save()) {
                    echo Json::encode(["status" => true, "message" => "修改级别成功"]);
                } else {
                    echo Json::encode(["status" => false, "message" => "修改级别失败"]);
                }
            } else {
                echo Json::encode(["status" => false, "message" => "请您选择你要修改的级别！"]);
            }
        }
    }

    /*
     * 账户余额转入可提现账户
     */

    public function actionShifttodrawal() {
        if (Yii::$app->request->isAjax) {
            $userid = Yii::$app->request->post("id");
            $number = Yii::$app->request->post("number");
            if ($number && $number > 0) {
                $user = MY_User::findOne($userid);
                if ($user->froststatus == 0) {
                    $useraccount = \backend\models\MY_UserAccountHy::find()->where("userid=:userid", [":userid" => $userid])->one();
                    if ($useraccount->amount >= $number) {
                        $trans = Yii::$app->db->beginTransaction();
                        try {
                            $useraccount->amount = $useraccount->amount - $number;
                            $useraccount->can_drawal_amount = $useraccount->can_drawal_amount + $number;
                            $userpayrecord = new \frontendm\models\WB_UserPayRecordHy();
                            $userpayrecord->userid = $userid;
                            $userpayrecord->type = 1;
                            $userpayrecord->class_type = 1;
                            $userpayrecord->amount = $number;
                            $userpayrecord->is_drawal = 1;
                            $userpayrecord->description = "【管理员" . Yii::$app->user->identity->username . "】将该账号的余额【" . $number . "】转入可提现账户";
                            if ($useraccount->save() && $userpayrecord->save()) {
                                $trans->commit();
                                echo Json::encode(["status" => true, "message" => "转入成功"]);
                            } else {
                                echo Json::encode(["status" => true, "message" => "转入失败"]);
                            }
                        } catch (Exception $ex) {
                            $trans->rollBack();
                            throw new \yii\web\NotFoundHttpException;
                        }
                    } else {
                        echo Json::encode(["status" => false, "message" => "该账户的余额小于转入数量，请核对账户余额再转入！"]);
                    }
                } else {
                    echo Json::encode(["status" => false, "message" => "该账户的处于【" . MY_User::$froststatus[$user->froststatus] . "】状态，不能进行余额转入操作！"]);
                }
            } else {
                echo Json::encode(["status" => false, "message" => "您填写的转入数量不正确！"]);
            }
        }
    }

    /*
     * 冻结账号
     */

    public function actionFrost() {
        $ids = Yii::$app->request->post("id");
        $pk = strpos($ids, ",") !== false ? explode(",", $ids) : array($ids);
        $number = Yii::$app->request->post("number");
        $note = Yii::$app->request->post("note");
        if ((int) $number > 0) {
            if ($note) {
                $trans = Yii::$app->db->beginTransaction();
                try {
                    $frosted_at = time() + $number * 86400;
                    foreach ($pk as $id) {
                        $user = MY_User::findOne($id);
                        if ($user instanceof MY_User) {
                            if ($user->froststatus == 0) {
                                $user->froststatus = 1;
                                $frostRecord = new \backend\models\MY_UserFrostRecord();
                                $frostRecord->userid = $user->id;
                                $frostRecord->username = $user->username;
                                $frostRecord->froststatus = 1;
                                $frostRecord->frosted_at = $frosted_at;
                                $frostRecord->note = "【" . $note . "】的原因，管理员【" . Yii::$app->user->identity->username . "】冻结了账号【" . $user->username . "】,解冻时间" . Yii::$app->formatter->asDatetime($frosted_at);
                                $user->save() && $frostRecord->save();
                            } else {
                                echo Json::encode(["status" => false, "message" => "您选择的账号【" . $user->username . "】已经被冻结或拉黑！，请不要再冻结此账号！"]);
                                exit;
                            }
                        }
                    }
                    $trans->commit();
                    echo Json::encode(["status" => true]);
                } catch (Exception $ex) {
                    $trans->rollBack();
                    throw new \yii\web\NotFoundHttpException;
                }
            } else {
                echo Json::encode(["status" => false, "message" => "请填写冻结说明"]);
            }
        } else {
            echo Json::encode(["status" => false, "message" => "请填写冻结期限"]);
        }
    }

    /*
     * 拉黑账号
     */

    public function actionFrost2() {
        $ids = Yii::$app->request->post("id");
        $pk = strpos($ids, ",") !== false ? explode(",", $ids) : array($ids);
        $note = Yii::$app->request->post("note");
        if ($note) {
            $trans = Yii::$app->db->beginTransaction();
            try {
                foreach ($pk as $id) {
                    $user = MY_User::findOne($id);
                    if ($user instanceof MY_User) {
                        if ($user->froststatus == 0) {
                            $user->froststatus = 2;
                            $frostRecord = new \backend\models\MY_UserFrostRecord();
                            $frostRecord->userid = $user->id;
                            $frostRecord->username = $user->username;
                            $frostRecord->froststatus = 2;
                            $frostRecord->note = "【" . $note . "】的原因，管理员【" . Yii::$app->user->identity->username . "】拉黑了账号【" . $user->username . "】";
                            $user->save() && $frostRecord->save();
                        } else {
                            echo Json::encode(["status" => false, "message" => "您选择的账号【" . $user->username . "】已经被冻结或拉黑！，请不要再拉黑此账号！"]);
                            exit;
                        }
                    }
                }
                $trans->commit();
                echo Json::encode(["status" => true]);
            } catch (Exception $ex) {
                $trans->rollBack();
                throw new \yii\web\NotFoundHttpException;
            }
        } else {
            echo Json::encode(["status" => false, "message" => "请填写拉黑说明"]);
        }
    }

    /*
     * 解封账号
     */

    public function actionDeblocking() {
        $ids = Yii::$app->request->post("id");
        $pk = strpos($ids, ",") !== false ? explode(",", $ids) : array($ids);
        $trans = Yii::$app->db->beginTransaction();
        try {
            foreach ($pk as $id) {
                $user = MY_User::findOne($id);
                if ($user instanceof MY_User) {
                    if ($user->froststatus == 2) {
                        $user->froststatus = 0;
                        $frostRecord = new \backend\models\MY_UserFrostRecord();
                        $frostRecord->userid = $user->id;
                        $frostRecord->username = $user->username;
                        $frostRecord->froststatus = 0;
                        $frostRecord->note = "管理员【" . Yii::$app->user->identity->username . "】解封了账号【" . $user->username . "】";
                        $user->save() && $frostRecord->save();
                    } else {
                        echo Json::encode(["status" => false, "message" => "您选择的账号【" . $user->username . "】不是拉黑状态！，您不能解封此账号！"]);
                        exit;
                    }
                }
            }
            $trans->commit();
            echo Json::encode(["status" => true]);
        } catch (Exception $ex) {
            $trans->rollBack();
            throw new \yii\web\NotFoundHttpException;
        }
    }

}
